package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.adapter.NumericWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.hjq.widget.view.SwitchButton;
import com.lxj.xpopup.core.BottomPopupView;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class BottomDateDialog  extends BottomPopupView {

    private BottomDateDialog.DialogClickListener mDialogClickListener;
    private TextView tvLabel;
    private SwitchButton btnUndisturbedSwitch;
    private WheelView wv_first;
    private WheelView wv_second;
    private WheelView wv_third;
    private WheelView wv_four;
    private WheelView wv_fifth;
    private WheelView wv_sixth;
    private NumericWheelAdapter mFirstNumericWheelAdapter;
    private NumericWheelAdapter mSecondNumericWheelAdapter;
    private NumericWheelAdapter mThirdNumericWheelAdapter;
    private NumericWheelAdapter mFourNumericWheelAdapter;
    private NumericWheelAdapter mFifthNumericWheelAdapter;
    private NumericWheelAdapter mSixthNumericWheelAdapter;
    private Calendar beignDate;
    private Calendar endDate;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.sss");
    private SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    private String showType;

    public BottomDateDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_bottom_date;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        beignDate = Calendar.getInstance();
        endDate = Calendar.getInstance();
        tvLabel = findViewById(R.id.tv_label);
        btnUndisturbedSwitch = findViewById(R.id.btn_undisturbed_switch);
        wv_first = findViewById(R.id.options1);
        wv_second = findViewById(R.id.options2);
        wv_third = findViewById(R.id.options3);
        wv_four = findViewById(R.id.options4);
        wv_fifth = findViewById(R.id.options5);
        wv_sixth = findViewById(R.id.options6);
        View btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    String start = wv_first.getAdapter().getItem(wv_first.getCurrentItem()) + "/" + wv_second.getAdapter().getItem(wv_second.getCurrentItem()) + "/" + wv_third.getAdapter().getItem(wv_third.getCurrentItem());
                    String end = wv_four.getAdapter().getItem(wv_four.getCurrentItem()) + "/" + wv_fifth.getAdapter().getItem(wv_fifth.getCurrentItem()) + "/" + wv_sixth.getAdapter().getItem(wv_sixth.getCurrentItem());
                    mDialogClickListener.onConfirmClick(start, end);
                }
            }
        });
        setStyle();
        initWheelView();
    }

    private void setStyle() {
        wv_first.setGravity(Gravity.CENTER);
        wv_first.setCyclic(false);
        wv_first.setAlphaGradient(true);
//        wv_first.setLabelOffset(5);
        wv_first.setItemsVisibleCount(5);
        wv_first.setDividerColor(0x00000000);
        wv_first.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_first.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_second.setGravity(Gravity.CENTER);
        wv_second.setCyclic(false);
        wv_second.setAlphaGradient(true);
//        wv_second.setLabelOffset(5);
        wv_second.setItemsVisibleCount(5);
        wv_second.setDividerColor(0x00000000);
        wv_second.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_second.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_third.setGravity(Gravity.CENTER);
        wv_third.setCyclic(false);
        wv_third.setAlphaGradient(true);
//        wv_third.setLabelOffset(5);
        wv_third.setItemsVisibleCount(5);
        wv_third.setDividerColor(0x00000000);
        wv_third.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_third.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_four.setGravity(Gravity.CENTER);
        wv_four.setCyclic(false);
        wv_four.setAlphaGradient(true);
//        wv_third.setLabelOffset(5);
        wv_four.setItemsVisibleCount(5);
        wv_four.setDividerColor(0x00000000);
        wv_four.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_four.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_fifth.setGravity(Gravity.CENTER);
        wv_fifth.setCyclic(false);
        wv_fifth.setAlphaGradient(true);
//        wv_third.setLabelOffset(5);
        wv_fifth.setItemsVisibleCount(5);
        wv_fifth.setDividerColor(0x00000000);
        wv_fifth.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_fifth.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_sixth.setGravity(Gravity.CENTER);
        wv_sixth.setCyclic(false);
        wv_sixth.setAlphaGradient(true);
//        wv_third.setLabelOffset(5);
        wv_sixth.setItemsVisibleCount(5);
        wv_sixth.setDividerColor(0x00000000);
        wv_sixth.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_sixth.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));
    }

    private void initWheelView(){
        rebuildPicker();
        wv_first.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int selectedYear = Integer.valueOf((String) mFirstNumericWheelAdapter.getItem(wv_first.getCurrentItem()));
                beignDate.set(Calendar.YEAR,selectedYear);
                rebuildPicker();
            }
        });
        wv_second.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int selecteMonth = Integer.valueOf((String) mSecondNumericWheelAdapter.getItem(wv_second.getCurrentItem()));
                beignDate.set(Calendar.MONTH, selecteMonth - 1);
                rebuildPicker();
            }
        });
        wv_third.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int selecteDay = Integer.valueOf((String) mThirdNumericWheelAdapter.getItem(wv_third.getCurrentItem()));
                beignDate.set(Calendar.DAY_OF_MONTH,selecteDay);
                rebuildPicker();
            }
        });

        wv_four.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int selecteHour = Integer.valueOf((String) mFourNumericWheelAdapter.getItem(wv_four.getCurrentItem()));
                endDate.set(Calendar.YEAR,selecteHour);
                rebuildPicker();
            }
        });

        wv_fifth.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int selecteMin = Integer.valueOf((String) mFifthNumericWheelAdapter.getItem(wv_fifth.getCurrentItem()));
                endDate.set(Calendar.MONTH,selecteMin - 1);
                rebuildPicker();
            }
        });

        wv_sixth.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                int selecteMin = Integer.valueOf((String) mSixthNumericWheelAdapter.getItem(wv_sixth.getCurrentItem()));
                endDate.set(Calendar.DAY_OF_MONTH,selecteMin);
                rebuildPicker();
            }
        });
    }

    private void rebuildPicker(){
        Calendar currentCalendar = Calendar.getInstance();
        mFirstNumericWheelAdapter  = new NumericWheelAdapter(currentCalendar.get(Calendar.YEAR), currentCalendar.get(Calendar.YEAR) + 10);
        wv_first.setAdapter(mFirstNumericWheelAdapter);
        wv_first.setCurrentItem(beignDate.get(Calendar.YEAR)  - currentCalendar.get(Calendar.YEAR));

        int minMonth = 1;
        if(currentCalendar.get(Calendar.YEAR) == beignDate.get(Calendar.YEAR)){
            minMonth = currentCalendar.get(Calendar.MONTH) + 1;
        }
        mSecondNumericWheelAdapter  = new NumericWheelAdapter(minMonth, 12);
        wv_second.setAdapter(mSecondNumericWheelAdapter);
        wv_second.setCurrentItem(beignDate.get(Calendar.MONTH)+1 - minMonth );

        int minDay = 1;
        if(currentCalendar.get(Calendar.YEAR) == beignDate.get(Calendar.YEAR) && currentCalendar.get(Calendar.MONTH) == beignDate.get(Calendar.MONTH)){
            minDay = currentCalendar.get(Calendar.DAY_OF_MONTH);
        }
        mThirdNumericWheelAdapter  = new NumericWheelAdapter(minDay, beignDate.getActualMaximum(Calendar.DAY_OF_MONTH));
        wv_third.setAdapter(mThirdNumericWheelAdapter);
        if(beignDate.get(Calendar.DAY_OF_MONTH) < minDay){
            wv_third.setCurrentItem(0);
        }else{
            wv_third.setCurrentItem(beignDate.get(Calendar.DAY_OF_MONTH)-minDay);
        }


        mFourNumericWheelAdapter  = new NumericWheelAdapter(beignDate.get(Calendar.YEAR), beignDate.get(Calendar.YEAR) + 10);
        wv_four.setAdapter(mFourNumericWheelAdapter);
        wv_four.setCurrentItem(endDate.get(Calendar.YEAR)  - beignDate.get(Calendar.YEAR));

        int endMinMonth = 1;
        if(beignDate.get(Calendar.YEAR) == endDate.get(Calendar.YEAR)){
            endMinMonth = beignDate.get(Calendar.MONTH) + 1;
        }
        mFifthNumericWheelAdapter  = new NumericWheelAdapter(endMinMonth, 12);
        wv_fifth.setAdapter(mFifthNumericWheelAdapter);
        wv_fifth.setCurrentItem(endDate.get(Calendar.MONTH)+1 - endMinMonth );

        int endMinDay = 1;
        if(endDate.get(Calendar.YEAR) == beignDate.get(Calendar.YEAR) && endDate.get(Calendar.MONTH) == beignDate.get(Calendar.MONTH)){
            endMinDay = beignDate.get(Calendar.DAY_OF_MONTH);
        }
        mSixthNumericWheelAdapter  = new NumericWheelAdapter(endMinDay, endDate.getActualMaximum(Calendar.DAY_OF_MONTH));
        wv_sixth.setAdapter(mSixthNumericWheelAdapter);
        if(endDate.get(Calendar.DAY_OF_MONTH) < endMinDay){
            wv_sixth.setCurrentItem(0);
        }else{
            wv_sixth.setCurrentItem(endDate.get(Calendar.DAY_OF_MONTH)-endMinDay);
        }
    }

    public void setSelectDate(String beginDate,String endDate){
        if(TextUtils.isEmpty(beginDate)){
            return;
        }
        try {
            Date current = simpleDateFormat.parse(beginDate);
            this.beignDate = Calendar.getInstance();
            this.beignDate.setTime(current);
            current = simpleDateFormat.parse(endDate);
            this.endDate = Calendar.getInstance();
            this.endDate.setTime(current);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(wv_fifth != null){
            rebuildPicker();
        }
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable BottomDateDialog.DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(String startTime, String endTime);
    }
}
